home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 43
/
Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso
/
-serious-
/
comms
/
other
/
ascan
/
sources
/
main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-06-14
|
8KB
|
309 lines
// slink LIB:c.o main.o getopt.o utils.o errors.o ascan.o ddelay.o noopentcp.o to ascan LIB LIB:sc.lib LIB:miami.lib SC SD ND NOICONS
#include "ascan.h"
#include <dos/dostags.h>
#include "getopt.h"
#include "ascan_rev.h"
/***************************************************************************/
#define USAGE_MSG "Usage: %s [-Vhvn] [-c childs] [-p ppt] [-t timeout] <host> [from] [to]\n"
#define HELP_MSG USAGE_MSG "\
-V version, copyright and greets\n\
-h this help\n\
-v verbose mode (off)\n\
-n output service name (off)\n\
-c childs (%ld)\n\
-p ports per group (%ld)\n\
-t general timeout in seconds (%ld)\n\
host host to scan\n\
from from port (%ld)\n\
to to port (from port if specified, %ld otherwise)\n\n"
#define VERSION_MSG VERS " (" DATE ") © alfie\n\
Amiga multi process scanner.\n\
Thanks to:\n\
ircnet/#amyita friends.\n\
all the pc users ascanned on testing.\n"
/***************************************************************************/
static char _version[] = VERSTAG;
static char _author[] = "Alfonso Ranieri <alfier@iol.it>";
/***************************************************************************/
/*This is the main entry point */
/*"Real programmer blah blah blah" - I love "goto" and I use it :) */
int main(int argc,char **argv)
{
struct global g;
struct dstruct ds;
char *endptr = NULL;
u_long host;
struct MsgPort reply;
struct childMsg *msga, *msg;
register struct Library *MiamiBase;
register LONG sigBit;
register ULONG flags;
register int x, t, n, ts, f, ended, opt, from, to, gt,
nchild, ppt, res = RETURN_ERROR;
/*hummm just compile with no-break option, but I always forget it so... :)*/
signal(SIGINT,SIG_IGN);
/*must be freed*/
sigBit = -1;
memset(&ds,NULL,sizeof(ds));
/* defaults */
nchild = DEF_NCHILD;
gt = DEF_GT;
ppt = DEF_PPT;
from = DEF_FROM;
to = DEF_TO;
flags = 0;
while ((opt = getopt(argc,argv,"Vhvnc:p:t:"))!=-1)
{
switch (opt)
{
case 'V':
fprintf(stderr,VERSION_MSG);
res = RETURN_OK;
goto exit;
break;
case 'h':
fprintf(stderr,HELP_MSG,PRG,DEF_NCHILD,DEF_PPT,DEF_GT,DEF_FROM,DEF_TO);
if (opt=='h') res = RETURN_OK;
goto exit;
break;
case 'v':
flags |= FLG_VERBOSE;
break;
case 'n':
flags |= FLG_PORTNAME;
break;
case 'c':
nchild = strtoul(optarg,&endptr,10);
if ((*endptr != '\0') || (nchild<1) || (nchild>MAXCHILDS))
{
fprintf(stderr,"%s: bad childs value '%s'\n",PRG,optarg);
goto exit;
}
break;
case 'p':
ppt = strtoul(optarg,&endptr,10);
if ((*endptr != '\0') || (ppt<1) || (ppt>MAX_SOCKS_PER_TASK))
{
fprintf(stderr,"%s: bad ports per group value '%s'\n",PRG,optarg);
goto exit;
}
break;
case 't':
gt = strtoul(optarg,&endptr,10);
if ((*endptr != '\0') || (gt<0))
{
fprintf(stderr,"%s: bad general timeout '%s'\n",PRG,optarg);
goto exit;
}
break;
default:
fprintf(stderr,USAGE_MSG,PRG);
res = RETURN_WARN;
goto exit;
break;
}
}
argc -= optind;
argv += optind;
if (argc==0 || argc>3)
{
if (argc) fprintf(stderr,"%s: too many arguments.\n",PRG);
fprintf(stderr,USAGE_MSG,PRG);
goto exit;
}
if (**argv=='?' && *((*argv)+1)==0)
{
fprintf(stderr,USAGE_MSG,PRG);
res = RETURN_OK;
goto exit;
}
if ((host = resolve(*argv))==INADDR_NONE)
{
if (SetSignal(0,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
error(SCAN_ERROR_BREAK,PRG,NULL);
else error(SCAN_ERROR_TARGETNOTFOUND,PRG,*argv);
goto exit;
}
if (argc>1)
{
from = strtoul(*(argv+1),&endptr,10);
if ((*endptr != '\0') || (from<1) || (from>65536))
{
fprintf(stderr,"%s: bad from port value '%s'\n",PRG,*(argv+1));
goto exit;
}
if (argc==3)
{
to = strtoul(*(argv+2),&endptr,10);
if ((*endptr != '\0') || (to<1) || (to>65536))
{
fprintf(stderr,"%s: bad to port value '%s'\n",PRG,*(argv+2));
goto exit;
}
if (to<from)
{
fprintf(stderr,"%s: bad ports range '%d-%d'\n",PRG,from,to);
goto exit;
}
}
else to=from;
}
/* Miami as a dinamic FD stuff, Genesis crashes if >64 sockets*/
if (MiamiBase=OpenLibrary("miami.library",0)) CloseLibrary(MiamiBase);
else flags|=FLG_LIMITED;
g.flags = flags;
g.nchild=nchild;
if ((ppt>LIMITED_SOCKETS) && (flags & FLG_LIMITED)) ppt=LIMITED_SOCKETS;
g.ppt=ppt;
g.from=from;
g.to=to;
g.gt=gt;
/*the only things we allocate is the messages array but might be static too*/
if (!(msga=AllocVec(sizeof(struct childMsg)*nchild,MEMF_PUBLIC|MEMF_CLEAR)) ||
((sigBit=AllocSignal(-1))==-1) ||
!setDStruct(&ds))
{
error(SCAN_ERROR_NOMEM,PRG,NULL);
res = RETURN_FAIL;
goto end;
}
INITPORT(&reply,sigBit);
n = to-from+1;
ts = n/nchild;
if (n%nchild) ts++;
f=from;
if (flags & FLG_VERBOSE) printGlobal(&g,*argv,host);
memset(g.childs,NULL,sizeof(g.childs));
/*let's launch "nchild" ascan processes*/
for (t = 0; t<nchild; t++)
{
/*user wants to stop?*/
if (SetSignal(0,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
{
SetSignal(SIGBREAKF_CTRL_C,SIGBREAKF_CTRL_C);
goto fin;
}
INITMESSAGE(MESSAGE(msga+t),&reply,sizeof(struct childMsg));
sprintf((msga+t)->name,"Ascan-Child[%lx/%ld]",FindTask(NULL),t);
(msga+t)->g = &g;
(msga+t)->host = host;
(msga+t)->tnum = t;
(msga+t)->from = f;
(msga+t)->to = ((f+=ts)>to) ? to : f-1;
if (*(g.childs+t) = CreateNewProcTags(NP_Entry, ascan,
NP_Input, Input(),
NP_Output, Output(),
NP_Name, NULL,
NP_CloseInput, FALSE,
NP_CloseOutput, FALSE,
NP_CopyVars, FALSE,
NP_StackSize, 8192,
TAG_DONE))
{
PutMsg(&(*(g.childs+t))->pr_MsgPort,(struct Message *)(msga+t));
}
else
{
error(SCAN_ERROR_NOMEM,PRG,NULL);
res = RETURN_FAIL;
goto fin;
}
if (flags & FLG_VERBOSE) fprintf(stderr,"%s: child %s started\n",PRG,(msga+t)->name);
}
/*the global timeout*/
if (gt) startDelay(&ds,gt,0);
fin:
for (ended = 0; t>0;)
{
/*we wait for ctrl_c timeout or an ascan process end*/
res = Wait(SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|(1<<sigBit)|(1<<ds.sigBit));
/*a process exited?*/
while (msg = (struct childMsg *)GetMsg(&reply))
{
if (msg->res)
{
if ((msg->res!=SCAN_ERROR_BREAK) || (flags & FLG_VERBOSE))
error(msg->res,msg->name,(STRPTR)msg->code);
}
else if (flags & FLG_VERBOSE) fprintf(stderr,"%s ended\n",msg->name);
*(g.childs+msg->tnum) = NULL;
t--;
}
/*not implemeted...each process should print infos*/
if (!ended && (res & SIGBREAKF_CTRL_D))
{
Forbid();
for (x = 0; x<nchild; x++)
if (*(g.childs+x)) Signal((struct Task *)*(g.childs+x),SIGBREAKF_CTRL_D);
Permit();
}
/*ctrl-c or timeout?*/
if (!ended && (res & (SIGBREAKF_CTRL_C|(1<<ds.sigBit))))
{
/*if so we stop each stil running ascan*/
if (res & SIGBREAKF_CTRL_C) error(SCAN_ERROR_BREAK,PRG,NULL);
else error(SCAN_ERROR_TIMEOUT,PRG,NULL);
Forbid();
for (x = 0; x<nchild; x++)
if (*(g.childs+x)) Signal((struct Task *)*(g.childs+x),SIGBREAKF_CTRL_C);
Permit();
ended = 1;
}
}
fprintf(stderr,"\n%s: done.\n",PRG);
res = RETURN_OK;
end:
freeDStruct(&ds);
if (sigBit!=-1) FreeSignal(sigBit);
if (msga) FreeVec(msga);
exit:
exit(res);
}
/***************************************************************************/